/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.duarteduarte.li3;

import java.util.ArrayList;
import org.duarteduarte.li3.util.Factories;
import org.duarteduarte.li3.processing.FileHandling;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.duarteduarte.li3.data.Artigo;
import org.duarteduarte.li3.data.CoAutor;
import org.duarteduarte.li3.processing.Storage;
import org.duarteduarte.li3.processing.Statistics;

/**
 *
 * @author duarteduarte
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here;
        Storage store = new Storage();
        //store.setFileName("/Users/duartenunoferreiraduarte/Desktop/publicx.txt");
        store.setFileName("C:\\Users\\Pedro\\Desktop\\publicx.txt");
        Statistics statistics;
        int linhaNum = 0;
//        store.testAutores();
//        try {
//            FileHandling.writeToFile(store.getAutoresMap());
//            store.setAutoresMap(FileHandling.readFromFile());
//        } catch (IOException ioe) {
//            System.out.println("IOE: " + ioe.getMessage());
//        } catch (ClassNotFoundException cnfe) {
//            System.out.println("CNFE: " + cnfe.getMessage());
//        }


        //List<String> linhas = FileHandling.leLinhasScanner("/Users/duartenunoferreiraduarte/Desktop/publicx.txt");
        List<String> linhas = FileHandling.leLinhasScanner(store.getRedeAutores().getNomeFicheiro());
        int i = 0;
        int last = 0;

        Iterator<String> linhasIt = linhas.iterator();
        while (linhasIt.hasNext()) {
            if (i++ - last == 10000) {
                last = i;
                System.out.println(last);
            }

            String linha = linhasIt.next();
            if (linha != null && !linha.isEmpty()) {
                StringTokenizer st = new StringTokenizer(linha, ",");

                List<String> tokens = Factories.giveMeList();
                while (st.hasMoreTokens()) {
                    tokens.add(st.nextToken());
                }

                String anoString = tokens.get(tokens.size() - 1).trim();
                int ano = 0;
                if (anoString.matches("^[0-9]+$")) {
                    ano = Integer.parseInt(anoString);
                }
                // System.out.println(linhaNum++);
                tokens.remove(tokens.size() - 1);

                Iterator<String> autoresIt = tokens.iterator();
                Artigo tmp = new Artigo();
                while (autoresIt.hasNext()) {
                    tmp.insereAutor(autoresIt.next().trim());
                }
                store.addArtigo(ano, tmp);
            }
        }


        statistics = new Statistics(store.getRedeAutores());
        //System.out.println("Autores: " + store.getRedeAutores().toString());
        //System.out.println("Numero Artigos: " + store.getArtigosAno(2003).getNumeroArtigos());

        ArrayList<String> aux = new ArrayList<String>();
        aux.add("Kazunori Komatani");
        aux.add("Tetsuya Ogata");
        aux.add("Hiroshi G. Okuno");

        System.out.println("pergunta1.1. -----> " + statistics.pergunta11());
        System.out.println(statistics.pergunta12());
        System.out.println(statistics.pergunta13());
        System.out.println(statistics.pergunta21a(10, 1900, 2020));
        System.out.println(statistics.pergunta21d(1995, 2005));
        System.out.println(statistics.pergunta22a());
        for (CoAutor ca : statistics.pergunta21c(aux, 1968, 2013)) {
            System.out.println(ca.getNome());
        }
        System.out.println(statistics.pergunta21b(20, 1968, 2013));
        System.out.println(statistics.pergunta22b(3, 1968, 2013));
    }

    public int max(List<Integer> lista) {
        int res = Integer.MIN_VALUE;

        for (Iterator<Integer> it = lista.iterator(); it.hasNext();) {
            int x = it.next();
            res = x > res ? x : res;
        }

        return res;
    }
}
